#pragma once
#include <iostream>
#include <vector>
using namespace std;


namespace lr {
	template<class T, class Container = vector<T>, class Compare = greater<T>>
	class priority_queue {
	public:
		priority_queue() {}

		template <class InputIterator>
		priority_queue(InputIterator first, InputIterator last) {
			_con(first, last);
			for (int i = (_con.size() - 2) / 2;i > 0;i--) {
				adjust_up(i);
			}
		}
		void adjust_up(int child) { 
			Compare com;
			//while (_con[child] > _con[(child - 1) / 2]) {
			while (com(_con[(child - 1) / 2],_con[child])) {
				swap(_con[child], _con[(child - 1) / 2]);
				child = (child - 1) / 2;
			}
		}
		void adjust_down(size_t parent) {
			Compare com;
			size_t left = parent*2 + 1;
			while (left < _con.size()) {
				//size_t more = left + 1 < _con.size() && _con[left + 1] > _con[left]

				size_t more = left + 1 < _con.size() && com(_con[left], _con[left+1])
					? left + 1 : left;
				//more = _con[parent] > _con[more] ? parent : more;
				more = com(_con[parent],_con[more]) ? more : parent;
				if (more == parent) {
					break;
				}
				swap(_con[parent], _con[more]);
				parent = more;
				left = parent * 2 + 1;
			}
		}

		void push(const T& val) {
			_con.push_back(val);
			adjust_up(_con.size() - 1);
		}
		T& top() {
			return _con[0];
		}
		void pop() {
			swap(_con[0], _con[_con.size()-1]);
			_con.pop_back();
			adjust_down(0);
		}

		bool empty() {
			return _con.empty();
		}

		size_t size() {
			return _con.size();
		}
	private:
		Container _con;
	};
}

